const dbutil = require('../../../libs/MyDB');
const uuid = require('node-uuid');

module.exports = {
    addOrder: async function (data) {
        let platform = '622'; //平台变化 622代表当前系统平台的码
        let r1 = Math.floor(Math.random()*10); // 获取两个0-9的随机数
        let r2 = Math.floor(Math.random()*10);
        let sysDate = new Date().Format('yyyyMMddhhmmss'); // 生成系统时间
        let order_number = platform+r1+sysDate+r2; // 生成订单编号
        let order = {
            order_number:order_number,
            order_payment_status:'未提交',
            user_id:data.user_id,
            table_id:data.table_id
        };
        let orderObj = {}
        await dbutil.add('res_order',order)
        .then(
           res =>{
            orderObj = res;
           }
        )
        return {
            code: 200,
            msg: "执行成功",
            order:orderObj
        }
    },
    addOrderFood: async function(data){
        let food = {};
        await dbutil.singleSelect('res_order_food','*',` order_id=${data.order_id} and food_id=${data.food_id} and order_food_state='未提交' `)
        .then(
            res => {
                food = res;
            }
        )
        console.log(food);
        if(food.length == 0){
            data.order_food_state = '未提交'
            await dbutil.add('res_order_food',data)
            
        }else{
            data.food_price = data.food_price/data.food_count;
            data.food_count += 1;
            data.food_price = data.food_price * data.food_count;
            await dbutil.update('res_order_food',` order_id=${data.order_id} and food_id=${data.food_id} `,data)
        }
        
        return {
            code: 200,
            msg: "执行成功"
        }
    },
    updataOrderFood: async function(data){
        await dbutil.update('res_order_food',` order_id=${data.order_id} and food_id=${data.food_id} `,data)
        let orderFoods = [];
        let sql = `select res_food.*,res_order_food.food_count,res_order_food.food_price as order_food_price from res_order_food LEFT JOIN res_food on res_order_food.food_id = res_food.food_id where order_id = ${data.order_id}`;
        await dbutil.sqlSelect(sql)
            .then(
                result => {
                    orderFoods = result;
                }
            )
        return {
            code: 200,
            msg: "执行成功",
            data: orderFoods,
        }
    },
    delOrderFood: async function(data){
        await dbutil.del('res_order_food',` order_id=${data.order_id} and food_id=${data.food_id} `)
        let orderFoods = [];
        let sql = `select res_food.*,res_order_food.food_count,res_order_food.food_price as order_food_price from res_order_food LEFT JOIN res_food on res_order_food.food_id = res_food.food_id where order_id = ${data.order_id}`;
        await dbutil.sqlSelect(sql)
            .then(
                result => {
                    orderFoods = result;
                }
            )
        return {
            code: 200,
            msg: "执行成功",
            data: orderFoods,
        }
    },
    getOrderSelect: async function(selectId){
        let orderFoods = [];
        let sql = `select res_food.*,res_order_food.food_count,res_order_food.food_price as order_food_price,res_order_food.order_food_state from res_order_food LEFT JOIN res_food on res_order_food.food_id = res_food.food_id where order_id = ${selectId} and res_order_food.order_food_state <> '已提交' `;
        await dbutil.sqlSelect(sql)
            .then(
                result => {
                    orderFoods = result;
                }
            )
        return {
            code: 200,
            msg: "查询成功",
            data: orderFoods,
            count: orderFoods.length,
        }
    },
    getOrderInfo: async function(data){
        let orders = [];
        await dbutil.multiSelect('res_order','*',`user_id=${data.user_id} and order_payment_status = '已完成'`)
        .then(
            res=>{
                orders = res;
            }
        )
        for (let index = 0; index < orders.length; index++) {
            let element = orders[index];
            let orderFoods = [];
            let sql = `select res_food.*,res_order_food.food_count,res_order_food.food_price as order_food_price from res_order_food LEFT JOIN res_food on res_order_food.food_id = res_food.food_id where order_id = ${element.order_id}`;
            await dbutil.sqlSelect(sql)
                .then(
                    result => {
                        orderFoods = result;
                    }
                )
            orders[index].foods = orderFoods;
            let date = new Date(element.order_time);
            let y = date.getFullYear();
            let m = date.getMonth()+1;
            let d = date.getDate();
            let time = `${y}-${m<10?'0'+m:m}-${d<10?'0'+d:d}`;
            orders[index].order_time = time;
        }
        return {
            code: 200,
            msg: "查询成功",
            data: orders,
            count: orders.length,
        }
    },
    setOrder: async function(data){
        let date = new Date();
        let y = date.getFullYear();
        let m = date.getMonth()+1;
        let d = date.getDate();
        let time = `${y}-${m<10?'0'+m:m}-${d<10?'0'+d:d}`;
        let sql = `select b.*,a.food_count,a.food_price as order_food_price,a.order_food_state from res_order_food a LEFT JOIN res_food b on a.food_id = b.food_id where order_id = ${data.order_id}`;
        let orderFoods = [];
        await dbutil.sqlSelect(sql)
            .then(
                result => {
                    orderFoods = result;
                }
            )
        let food_price = 0;
        let food_count = 0;
        for (let index = 0; index < orderFoods.length; index++) {
            let element = orderFoods[index];
            food_price += element.order_food_price;
            food_count += element.food_count;
        }
        await dbutil.update('res_order',`order_id = ${data.order_id}`,{order_payment_status:'已完成',order_time:time,order_price:food_price,order_count:food_count})
        await dbutil.update('res_order_food',`order_id = ${data.order_id}`,{order_food_state:'已提交'})
        return {
            code: 200,
            msg: "执行成功",
        }
    }
}